/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wx.goodview.sample.slice;

import com.wx.goodview.GoodView;
import com.wx.goodview.LogUtil;
import com.wx.goodview.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.components.element.PixelMapElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.PopupDialog;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;

import java.io.IOException;

/**
 * 点赞效果示例
 */
public class MainAbilitySlice extends AbilitySlice {

    GoodView mGoodView;
    Image image111;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        image111 = new Image(this);
        Image image1 = (Image) findComponentById(ResourceTable.Id_good);
        Image image2 = (Image) findComponentById(ResourceTable.Id_good2);
        Image image3 = (Image) findComponentById(ResourceTable.Id_good3);
        Image image4 = (Image) findComponentById(ResourceTable.Id_good4);
        Button btnReset= (Button) findComponentById(ResourceTable.Id_btn_reset);
        image1.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtil.error(this.getClass().getName(),"1====show==="+component.getLocationOnScreen()[0]+","+component.getLocationOnScreen()[1]+",");
                mGoodView = new GoodView(MainAbilitySlice.this, image111, 30, 30);
                image1.setImageAndDecodeBounds(ResourceTable.Media_good_checked);
                mGoodView.setText("+1");
                int offsetX = component.getLocationOnScreen()[0] + 150;
                int offsetY = component.getLocationOnScreen()[1] + 80;
                mGoodView.show(component,offsetX,offsetY);
            }
        });
        image2.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                try {
                    LogUtil.error(this.getClass().getName(),"1====show==="+component.getLocationOnScreen()[0]+","+component.getLocationOnScreen()[1]+",");
                    mGoodView = new GoodView(MainAbilitySlice.this, image111, 100, 100);
                    image2.setImageAndDecodeBounds(ResourceTable.Media_good_checked);
                    mGoodView.setImage(new PixelMapElement(component.getResourceManager().getResource(ResourceTable.Media_good_checked)));
                    mGoodView.show(component,0,0);
                } catch (IOException e) {
                    LogUtil.error(this.getClass().getName(),"IOException:"+e);
                } catch (NotExistException e) {
                    LogUtil.error(this.getClass().getName(),"NotExistException:"+e);
                }

            }
        });

        image3.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtil.error(this.getClass().getName(),"1====show==="+component.getLocationOnScreen()[0]+","+component.getLocationOnScreen()[1]+",");
                mGoodView = new GoodView(MainAbilitySlice.this, image111, 120, 30);
                image3.setImageAndDecodeBounds(ResourceTable.Media_collection_checked);
                mGoodView.setTextInfo("收藏成功", 0xfff66467, 20);
                int offsetX = component.getLocationOnScreen()[0] + 120;
                int offsetY = component.getLocationOnScreen()[1] + 80;
                mGoodView.show(component,offsetX,offsetY);
            }
        });

        image4.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                LogUtil.error(this.getClass().getName(),"1====show==="+component.getLocationOnScreen()[0]+","+component.getLocationOnScreen()[1]+",");
                mGoodView = new GoodView(MainAbilitySlice.this, image111, 120, 30);
                image4.setImageAndDecodeBounds(ResourceTable.Media_bookmark_checked);
                mGoodView.setTextInfo("收藏成功", 0xffff941A, 20);
                int offsetX = component.getLocationOnScreen()[0] + 120;
                int offsetY = component.getLocationOnScreen()[1] + 80;
                mGoodView.show(component,offsetX,offsetY);
            }
        });

        btnReset.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                image1.setImageAndDecodeBounds(ResourceTable.Media_good);
                image2.setImageAndDecodeBounds(ResourceTable.Media_good);
                image3.setImageAndDecodeBounds(ResourceTable.Media_collection);
                image4.setImageAndDecodeBounds(ResourceTable.Media_bookmark);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
